package com.cct.digitalarchapi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.cct.digitalarchapi.common.CommonUtil;
import com.cct.digitalarchapi.common.Response;
import com.cct.digitalarchapi.pojo.RootList;
import com.cct.digitalarchapi.pojo.StaffList;
import com.cct.digitalarchapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @FileName UserController
 * @Description 用户控制器
 * @Author Created by chenmingzhe
 * @Date 2023/11/21
 **/
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CommonUtil commonUtil;
    /**
     * 管理员登录
     */
    @RequestMapping(value = "/loginByManager",method = RequestMethod.POST)
    public Response loginByManager(@RequestBody JSONObject obj){
        String username = obj.getString("username");
        String passwd = obj.getString("passwd");
        commonUtil.recordOperateLog("用户"+username+"登录了数字档案平台");
        return  userService.loginByManager(username,passwd);
    }
    /**
     * 用户登录
     */
    @RequestMapping(value = "/loginByUser",method = RequestMethod.POST)
    public Response loginByUser(@RequestBody JSONObject obj){
        String staffCode = obj.getString("username");
        String passwd = obj.getString("passwd");
        commonUtil.recordOperateLog("员工"+staffCode+"登录了数字档案平台（员工）");
        return  userService.loginByUser(staffCode,passwd);
    }
    /**
    *添加管理员
    */
    @RequestMapping(value = "/addManager",method = RequestMethod.POST)
    public Response addManager(@RequestBody RootList rootList){
        commonUtil.recordOperateLog("添加了用户名为"+rootList.getUsername()+"管理员");
        return  userService.addManager(rootList);
    }
    /**
    *添加用户
    */
    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public Response addUser(@RequestBody StaffList staffList){
        commonUtil.recordOperateLog("添加了员工编号为"+staffList.getStaffCode()+"用户");
        return  userService.addUser(staffList);
    }
    /**
     *删除管理员
     */
    @RequestMapping(value = "/deleteManager",method = RequestMethod.POST)
    public Response deleteManager(@RequestBody JSONObject obj){
        String username = obj.getString("username");
        commonUtil.recordOperateLog("删除了用户名为"+username+"管理员");
        return  userService.deleteManager(username);
    }

    /**
     *删除用户
     */
    @RequestMapping(value = "/deleteUser",method = RequestMethod.POST)
    public Response deleteUser(@RequestBody JSONObject obj){
        int staffId=obj.getInteger("staffId");
        commonUtil.recordOperateLog("删除了员工编号为"+staffId+"用户");
        return  userService.deleteUser(staffId);
    }

    /**
     *查询所有的档案管理员
     */
    @RequestMapping(value = "/qryAllManager",method = RequestMethod.POST)
    public Response qryAllManager(@RequestBody JSONObject obj){
        int pageNum=obj.getInteger("pageNum");
        int pageSize=obj.getInteger("pageSize");
        return  userService.qryAllManager(pageNum,pageSize);
    }

    /**
     *根据部门code查询所有的员工
     */
    @RequestMapping(value = "/qryUserByDept",method = RequestMethod.POST)
    public Response qryUserByDept(@RequestBody JSONObject obj){
        //传空则查全部
        String archDeptCode = obj.getString("archDeptCode");
        int pageNum=obj.getInteger("pageNum");
        int pageSize=obj.getInteger("pageSize");
        //传空则查全部，传值模糊搜索
        String staffName=obj.getString("staffName");
        //传空则查全部，传值模糊搜索
        String staffCode=obj.getString("staffCode");
        return  userService.qryUserByDept(archDeptCode,pageNum,pageSize,staffName,staffCode);
    }
    /**
    *更新管理员或员工的密码
    */
    @RequestMapping(value = "/updatePasswd",method = RequestMethod.POST)
    public Response updatePasswd(@RequestBody JSONObject obj){
        String username = obj.getString("username");
        String newPasswd = obj.getString("newPasswd");
        String oldPasswd = obj.getString("oldPasswd");
        //type为1修改管理员密码，为2修改员工密码
        String type = obj.getString("type");
        commonUtil.recordOperateLog("更新了密码");
        return  userService.updatePasswd(username,oldPasswd,newPasswd,type);
    }
    /**
    *更新档案管理员的真实名字
    */
    @RequestMapping(value = "/updateManager",method = RequestMethod.POST)
    public Response updateManager(@RequestBody RootList rootList){
        commonUtil.recordOperateLog("更新了管理员"+rootList.getUsername()+"的真实名字");
        return  userService.updateManager(rootList);
    }
    /**
    *更新员工的信息
    */
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public Response updateUser(@RequestBody StaffList staffList){
        commonUtil.recordOperateLog("更新了员工"+staffList.getStaffId()+"信息");
        return  userService.updateUser(staffList);
    }
    /**
     *重置管理员或员工的密码
     */
    @RequestMapping(value = "/resetPasswd",method = RequestMethod.POST)
    public Response resetPasswd(@RequestBody JSONObject obj){
        String username = obj.getString("username");
        //type为重置管理员密码，为2重置员工密码
        String type = obj.getString("type");
        commonUtil.recordOperateLog("重置了用户"+username+"的密码");
        return  userService.resetPasswd(username,type);
    }
    /**
    *获取员工号
    */
    @RequestMapping(value = "/getStaffCode",method = RequestMethod.POST)
    public Response getStaffCode(){
        return  userService.getStaffCode();
    }
    /**
     *获取员工的消息
     */
    @RequestMapping(value = "/getMessageByUser",method = RequestMethod.POST)
    public Response getMessageByUser(@RequestBody JSONObject obj){
        //username或staffCode
        String username = obj.getString("username");
        // -1 查所有,0-未读，1-已读
        int state =obj.getInteger("state");
        // 1档案管理员 2员工 3主管
        int type =obj.getInteger("type");
        int pageNum=obj.getInteger("pageNum");
        int pageSize=obj.getInteger("pageSize");
        return  userService.getMessageByUser(username,state,pageNum,pageSize,type);
    }

    /**
     *更新消息的状态
     */
    @RequestMapping(value = "/updateMessageState",method = RequestMethod.POST)
    public Response updateMessageState(@RequestBody JSONObject obj){
        //消息id
        int messId =obj.getInteger("messId");
        //0未读 1已读 送1
        int state=obj.getInteger("state");
        return  userService.updateMessageState(messId,state);
    }
    /**
    *获取系统信息
    */
    @RequestMapping(value = "/getSystemInfo",method = RequestMethod.POST)
    public Response getSystemInfo(){
        return  userService.getSystemInfo();
    }


    /**
     *上传水印
     */
    @RequestMapping(value="/uploadWaterMark",method= RequestMethod.POST)
    public Response uploadWaterMark(@RequestParam MultipartFile file) {
        if (file.isEmpty()) {
            return Response.bad("文件为空");
        }
        commonUtil.recordOperateLog("上传了最新的水印");
        return userService.uploadWaterMark(file);
    }


}
